﻿using System;
using Niknak.Services.ServiceCore.TaskScheduler;
using Niknak.Services.TaskScheduler.Interfaces;

namespace Niknak.Services.TaskScheduler
{
    /// <summary>
    /// Replicates the important properties of ScheduledTask for disconnected use
    /// </summary>
    public class AdvancedTaskConfig<ScheduledTaskType> : IAdvancedTaskConfig, ICustomTaskConfig
        where ScheduledTaskType : Interfaces.DataModel.IScheduledTask, new()
    {
        #region IAdvancedTaskConfig Members

        /// <summary>
        /// StartDate converted to string for compatability with ServiceCore
        /// </summary>
        public string StartDate { get; set; }
        /// <summary>
        /// EndDate converted to string for compatability with ServiceCore
        /// </summary>
        public string EndDate { get; set; }
        /// <summary>
        /// EndTime converted to string for compatability with ServiceCore
        /// </summary>
        public string EndTime { get; set; }
        public bool TerminateAtEndTime { get; set; }
        /// <summary>
        /// StartDate converted to string for compatability with ServiceCore
        /// </summary>
        public string StartTime { get; set; }
        public Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.IntervalType IntervalType { get; set; }
        /// <summary>
        /// Custom interval converted to string for compatability with ServiceCore
        /// </summary>
        public string CustomInterval { get; set; }
        public Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.DaysOfWeek RunOnDays { get; set; }
        public int AllowableDrift { get; set; }

        #endregion

        #region ICustomTaskConfig

        public Guid ScheduledTaskId { get; set; }
        public string Name { get; set; }
        public long CustomIntervalSeconds { get; set; }
        public string CLRType { get; set; }
        public string StartArguments { get; set; }
        public bool EnableCompletedEvent { get; set; }
        public bool EnableLogging { get; set; }

        #endregion

        public AdvancedTaskConfig() { }

        public AdvancedTaskConfig(ScheduledTaskType fromScheduledTask)
        {
            ScheduledTaskId = fromScheduledTask.ScheduledTaskId;
            Name = fromScheduledTask.Name;
            StartDate = fromScheduledTask.StartDate;
            EndDate = fromScheduledTask.EndDate;
            StartTime = fromScheduledTask.StartTime;
            EndTime = fromScheduledTask.EndTime;
            TerminateAtEndTime = fromScheduledTask.TerminateAtEndTime;
            IntervalType = (Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.IntervalType)fromScheduledTask.IntervalType;
            CustomInterval = TimeSpan.FromSeconds(fromScheduledTask.CustomIntervalSeconds).ToString();
            CustomIntervalSeconds = fromScheduledTask.CustomIntervalSeconds;
            RunOnDays = (Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.DaysOfWeek)fromScheduledTask.RunOnDays;
            CLRType = fromScheduledTask.CLRType;
            StartArguments = fromScheduledTask.StartArguments;
            AllowableDrift = fromScheduledTask.AllowableDrift;
            EnableCompletedEvent = fromScheduledTask.EnableCompletedEvent;
            EnableLogging = fromScheduledTask.EnableLogging;
        }
    }
}
